// @bfmeta/node-sdk
const mysql = require('mysql2');
const { BFMetaSDK } = require ("@bfmeta/node-sdk")
const { CryptoHelper} =  require ("@bfmeta/node-sdk/build/test/helpers/cryptoHelper");
const {FecthDemo} = require("@bfmeta/node-sdk/build/test/helpers/fecthDemo")
const {bfmetaConfig,mysqlConfig} = require('../config/config')
const multer = require('multer')


const cryptoHelper = new CryptoHelper();
const fecthDemo = new FecthDemo();
// 生成SDK实例
const bfmetaSDK = new BFMetaSDK(bfmetaConfig, { netType: "testnet", cryptoHelper}, fecthDemo);
// 生成数据库连接实例
const db = mysql.createPool(mysqlConfig).promise()

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'public/img')
  },
  filename: (request, file, callback) => {
    let fileExtension = file.originalname.split('.')[1];
    if(!fileExtension) fileExtension = ''
    const newNameFile = require('crypto').randomBytes(16).toString('hex')+'.png';

    callback(null, `${fileExtension}${newNameFile}`)
  }
});
const upload = multer({ storage });

module.exports = {bfmetaSDK, db, upload}